﻿using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.SalesManagement.Components.Order;
using Kevin.Shop.Business.SalesManagement.Enumerations;
using Kevin.Shop.Business.Service.Entity.Helper;
using Kevin.Shop.Business.Service.Entity.Products.Enumerations;
using Kevin.Shop.Business.Service.Entity.SalesManagement.Order;
using Kevin.Shop.Business.StoreManagement.Components.Store;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.ServiceEntity.MyAccount;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RiderShop.Mobile.Modle.MyAccount
{
    public class OrderModel
    {
        public int FiterIndex { get; set; }
        public List<OrderBasicInfo> OrderList { get; set; }

        public int OrderTotlaCount { get; set; }

        public int OrderTotlaPage { get; set; }
        /*
        <option value="1">近一个月订单</option>
        <option value="2">一个月前订单</option>
        <option value="4">近一个月取消的订单</option>
        <option value="4">一个月前取消的订单</option>
       */
        public Dictionary<string, int> SearchCondition
        {
            get
            {
                var list = new Dictionary<string, int>();
                list.Add("订单筛选:近一个月订单", 1);
                list.Add("订单筛选:一个月前订单", 2);
                list.Add("订单筛选:近一个月取消的订单", 3);
                list.Add("订单筛选:一个月前取消的订单", 4);
                return list;
            }
        }
        /// <summary>
        /// 获取订单集合
        /// </summary>
        /// <returns></returns>
        public DbQueryResult<List<OrderBasicInfo>> GetMyOrderList(int orderType, int pageIndex, int pageSize)
        {
            OrderQueryBasicInfo query = new OrderQueryBasicInfo();
            switch (orderType)
            {
                case 1://近一个月订单
                    query.OrderType = 1;
                    query.OrderBeginDate = DateTime.Now.AddMonths(-1).Date;
                    query.OrderEndDate = DateTime.Now.Date.AddDays(1).Date;
                    break;
                case 2://一个月前订单
                    query.OrderType = 1;
                    query.OrderEndDate = DateTime.Now.AddMonths(-1).Date;
                    break;
                case 3://近一个月取消的订单
                    query.OrderType = 2;
                    query.OrderBeginDate = DateTime.Now.AddMonths(-1).Date;
                    query.OrderEndDate = DateTime.Now.Date.AddDays(1).Date;
                    break;
                case 4://一个月前取消的订单
                    query.OrderType = 2;
                    query.OrderEndDate = DateTime.Now.AddMonths(-1).Date;
                    break;
            }
            query.InUser = WebClientContext.Current.UserId;
            query.CurrentUserId = WebClientContext.Current.UserId;
            query.PageIndex = pageIndex;
            query.PageSize = pageSize;
            DbQueryResult<List<OrderBasicInfo>> orderList = RestfulHelper.Query<OrderBasicInfo>(SiteUrl.FormatUrl("SalesManagement_GetUserRecentlyOrder"), query);

            foreach (var order in orderList.Data)
            {
                if (!string.IsNullOrEmpty(order.ProductNameList))
                {
                    string[] orderProductIDandNameList = order.ProductNameList.Split(';');
                    foreach (var orderProductIDandName in orderProductIDandNameList)
                    {
                        int qty = 1;
                        if (!string.IsNullOrEmpty(orderProductIDandName) && orderProductIDandName.Split(',').Length > 1)
                        {
                            string[] productIDandName = orderProductIDandName.Split(',');
                            string productID = productIDandName[0];
                            string productName = productIDandName[1];
                            if (productName.Split('x').Length > 1)
                            {
                                string[] list = productName.Split('x');
                                Int32.TryParse(list[0].Trim(), out qty);
                                productName = list[1];
                            }
                            if (!string.IsNullOrEmpty(productID) && !string.IsNullOrEmpty(productName))
                            {
                                order.ProductList.Add(new OrderProductInfo() { ProductId = productID, ProductName = productName, Quantity = qty });
                            }
                        }
                    }
                }
            }

            return orderList;
        }

        /// <summary>
        /// 获取订单明细
        /// </summary>
        /// <param name="orderNumber">订单编号</param>
        /// <returns></returns>
        public static OrderBasicInfoExtend GetOrderDetailInfo(string orderNumber)
        {

            OrderBasicInfoExtend order = RestfulHelper.QueryWithSingle<OrderBasicInfoExtend>(SiteUrl.FormatUrl("SalesManagement_GetOrderDetailInfo", orderNumber));
            order.OrderProcessLogList = RestfulHelper.Query<OrderProcessLog>(SiteUrl.FormatUrl("SalesManagement_GetOrderProcessLogList", orderNumber));
            foreach (var item in order.ProductList)
            {
                item.ImageName = SiteUrl.FormatProductImageUrl(ImageType.Size60, item.ProductId, item.ImageName);
            }
            order.FullRegionName = MyAccountModel.GetRegionFullName(order.RegionId);
            return order;
        }


        public ServiceResponser VoidOrder(string orderNumber, int voidType, string voidReason)
        {
            var processInfo = new OrderProcessInfo()
            {
                UserId = WebClientContext.Current.UserId,
                Type = 3,
                OrderNumber = orderNumber,
                OrderStatus = OrderStatus.用户手动取消,
                VoidReasonType = voidType,
                VoidReason = voidReason
            };
            ServiceResponser reponse = RestfulHelper.Update(SiteUrl.FormatUrl("SalesManagement_OrderProcessManual"), processInfo);
            return reponse;

        }


        public static int GetOrderCount(OrderStatus orderStatus)
        {
            int count = 0;
            OrderQueryBasicInfo query = new OrderQueryBasicInfo();

            query.InUser = WebClientContext.Current.UserId;
            query.CurrentUserId = WebClientContext.Current.UserId;
            query.PageIndex = 0;
            query.PageSize = 1000;
            //query.OrderStatus = orderStatus;
            DbQueryResult<List<OrderBasicInfo>> orderList = RestfulHelper.Query<OrderBasicInfo>(SiteUrl.FormatUrl("SalesManagement_GetUserRecentlyOrder"), query);

            if (orderList.Data != null)
            {
                if (orderStatus == OrderStatus.已发货)
                {
                    return orderList.Data.Where(o => o.OrderStatus == OrderStatus.已付款 || o.OrderStatus==OrderStatus.待出库 
                        || o.OrderStatus == OrderStatus.已出库 || o.OrderStatus == OrderStatus.已发货).Count();
                }
                return orderList.Data.Where(o => o.OrderStatus == orderStatus).Count();
            }

            return count;
        }
    }
}
